<?php

namespace Beike\Seller\Http\Controllers\Components;

use Beike\Seller\Models\SellerUser;
use Illuminate\Support\Facades\Route;
use Illuminate\View\Component;

class Sidebar extends Component
{
    public array $links = [];

    public ?array $currentLink;

    private ?SellerUser $sellerUser;

    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->sellerUser = current_seller_user();
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return mixed
     */
    public function render()
    {
        $preparedMenus = [
            [
                'route'    => 'home.index',
                'icon'     => 'bi bi-house',
                'code'     => 'home',
                'blank'    => false,
                'name'     => trans('seller::common.home'),
                'children' => [],
            ],
            [
                'name'     => trans('seller::common.products'),
                'route'    => 'products.index',
                'icon'     => 'bi bi-box-seam',
                'code'     => 'products',
                'blank'    => false,
                'children' => [
                    [
                        'name'     => trans('seller::common.products'),
                        'route'    => 'products.index',
                        'blank'    => false,
                    ],
                    [
                        'name'     => trans('seller::common.categories'),
                        'route'    => 'categories.index',
                        'blank'    => false,
                    ],
                ],
            ],
            [
                'name'     => trans('seller::common.orders'),
                'route'    => 'orders.index',
                'icon'     => 'bi bi-clipboard-check',
                'code'     => 'orders',
                'blank'    => false,
                'children' => [
                    [
                        'name'     => trans('seller::common.orders'),
                        'route'    => 'orders.index',
                        'blank'    => false,
                    ],
                    [
                        'name'     => trans('seller::common.rmas'),
                        'route'    => 'rmas.index',
                        'blank'    => false,
                    ],
                    [
                        'name'     => trans('seller::common.transactions'),
                        'route'    => 'transactions.index',
                        'blank'    => false,
                    ],
                    [
                        'name'     => trans('seller::common.withdraws'),
                        'route'    => 'withdraws.index',
                        'blank'    => false,
                    ],
                ],
            ],
            [
                'name'     => trans('seller::report.reports'),
                'route'    => 'reports_sale.index',
                'icon'     => 'bi bi-clipboard-check',
                'code'     => 'reports',
                'blank'    => false,
                'children' => [
                    [
                        'name'     => trans('seller::report.order_reports'),
                        'route'    => 'reports_sale.index',
                        'blank'    => false,
                    ],
                    [
                        'name'     => trans('seller::report.view_reports'),
                        'route'    => 'reports_view.index',
                        'blank'    => false,
                    ],
                ],
            ],
            [
                'name'     => trans('seller::common.users'),
                'route'    => 'users.index',
                'icon'     => 'bi bi-person',
                'code'     => 'users',
                'blank'    => false,
                'children' => [
                    [
                        'name'     => trans('seller::user.user'),
                        'route'    => 'users.index',
                        'blank'    => false,
                    ],
                ],
            ],
            [
                'name'     => trans('seller::common.shipping'),
                'route'    => 'shipping.index',
                'icon'     => 'bi bi-minecart-loaded',
                'code'     => 'shipping',
                'blank'    => false,
                'children' => [],
            ],
            [
                'name'     => trans('seller::common.seller_info'),
                'route'    => 'edit.index',
                'icon'     => 'bi bi-person-circle',
                'code'     => 'seller_info',
                'blank'    => false,
                'children' => [],
            ],
        ];

        $currentRoute = Route::getCurrentRoute()->getName();
        foreach ($preparedMenus as $menu) {
            $routeFirst  = explode('.', $menu['route'])[0] ?? '';
            $routePrefix = 'seller.' . $routeFirst;
            $active      = substr($currentRoute, 0, strlen($routePrefix)) === $routePrefix;
            $this->addLink($menu['code'], 0, $menu['name'], $menu['icon'], $menu['route'], $active, $menu['blank']);

            foreach ($menu['children'] as $child) {
                $routeFirst  = explode('.', $child['route'])[0] ?? '';
                $routePrefix = 'seller.' . $routeFirst;
                $active      = substr($currentRoute, 0, strlen($routePrefix)) === $routePrefix;
                $this->addLink($menu['code'], 1, $child['name'], '', $child['route'], $active, $menu['blank']);
            }

        }
        $this->currentLink = $this->getCurrentLink();

        return view('seller::components.sidebar');
    }

    /**
     * 获取二级菜单
     *
     * @return array|null
     */
    private function getCurrentLink(): array|null
    {
        foreach ($this->links as $link) {
            if ($link['active']) {
                return $link;
            }
        }

        return null;
    }

    /**
     * 添加后台顶部菜单链接
     *
     * @param $title
     * @param $route
     * @param false $active
     */
    private function addLink($menu, $isSubMenu, $title, $icon, $route, bool $active = false, $blank = false)
    {
        $url = seller_route($route);
        if ($isSubMenu) {
            $this->links[$menu]['children'][] = [
                'title'  => $title,
                'url'    => $url,
                'active' => $active,
                'blank'  => $blank,
            ];
            if ($active) {
                $this->links[$menu]['active'] = 1;
            }
        } else {
            $this->links[$menu] = [
                'title'    => $title,
                'icon'     => $icon,
                'url'      => $url,
                'active'   => $active,
                'children' => [],
                'blank'    => $blank,
            ];
        }
    }
}
